<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class user_model extends MY_Model
{
	public function userList($data = array(),$per_page,$limit){
        $where = " WHERE 1 ";
        if(!empty($data['starttime'])){
            $where .= " AND bu.RegisteTime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $where .= " AND bu.RegisteTime <= {$data['endtime']}";
        }
        if(!empty($data['name'])){
            $where .= " AND rb.name like '%{$data['name']}%'";
        }
        if(!empty($data['contact'])){
            $where .= " AND bu.Username = {$data['contact']} ";
        }
        if(!empty($data['sign'])){
            $where .= " AND bu.sign = '{$data['sign']}' ";
        }
        $limit = "LIMIT {$per_page},{$limit}";
        //获取简历id

     $sql = "SELECT
                    bu.AccountID,bu.Username,bu.RegisteTime,bu.Status,bu.avatar,bu.NickName,bu.sex,bu.last_use_time
                FROM
                    yy_user bu where
              bu.Status<>2 ORDER BY bu.RegisteTime desc {$limit} ";

          // $sql .= " WHERE re.id=(SELECT id FROM bp_resume_education WHERE rid=rb.id AND  graduatetime = (SELECT  MAX(graduatetime) FROM bp_resume_education )) or re.id="" AND bu.isDel<>1  {$where}";
        $query = $this->db->query($sql);
        return $query->result_array();
    }


    public function get_user_cnt($data = array()){
        $where = " WHERE 1 ";
        if(!empty($data['starttime'])){
            $where .= " AND bu.RegisteTime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $where .= " AND bu.RegisteTime <= {$data['endtime']}";
        }
        if(!empty($data['name'])){
            $where .= " AND rb.name like '%{$data['name']}%'";
        }
        if(!empty($data['contact'])){
            $where .= " AND bu.Username = {$data['contact']} ";
        }
        if(!empty($data['sign'])){
            $where .= " AND bu.sign = '{$data['sign']}' ";
        }
        //获取简历id

     $sql = "SELECT bu.AccountID,bu.Username,bu.RegisteTime,bu.Status,bu.avatar,bu.NickName,bu.sex FROM
                    yy_user bu where bu.Status<>2";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }


    //编辑状态
    public function edit_status($id,$status){
        $this->db->where("id",$id);
        $data = array(
            'status'=>$status
        );
        $res = $this->db->update("bp_feedback",$data);
        return $res;
    }



    public function getUserInfoById($id){
        $where = " ";
        $sql = "SELECT
                    bu.AccountID,bu.Username,bu.RegisteTime,bu.sign,bu.Status,rb.avatar,rb.name,rb.sex,rb.company,rb.position,rb.career,rb.country,rb.city,rb2.birthday,re.schoolname,re.profession,edu.name as degree,car.name as career,sal.name as salary
                FROM
                    bp_user bu
                LEFT JOIN bp_resume_baseinfo rb ON bu.AccountID = rb.AccountID
                LEFT JOIN  bp_resume_baseinfo2 rb2 ON bu.AccountID = rb2.AccountID
                LEFT JOIN bp_resume_education re ON rb.id=re.rid
                LEFT JOIN bp_education edu ON re.degree = edu.id
                LEFT JOIN bp_career car ON rb.career = car.id
                LEFT JOIN bp_salary sal ON rb.salary = sal.id";

        $sql .= " WHERE re.id=(SELECT id FROM bp_resume_education WHERE rid=rb.id AND  graduatetime = (SELECT  MAX(graduatetime) FROM bp_resume_education )) AND bu.isDel<>1  {$where} and bu.AccountID={$id}";
        $query = $this->db->query($sql);
        return $query->row_array();
    }

    //获取简历基本信息1的信息
    public function getBaseInfo($AccountID,$files = " * "){

        $where = " Where rb.AccountID = {$AccountID}";
        $sql = "SELECT
                    $files
                FROM
                    bp_user bu
                LEFT JOIN bp_resume_baseinfo rb ON bu.AccountID = rb.AccountID
                LEFT JOIN  bp_resume_baseinfo2 rb2 ON bu.AccountID = rb2.AccountID ";
        $sql .= $where;
        $query = $this->db->query($sql);
        return  $query->row_array();
    }

    //根据二级id获取 一级、二级信息
    public function getTwoGenerlInfo($id,$table){
        $temp = $this->getGeneralInfoById($id,$table);
        $result = array();
        if($temp){
            $sql  ="SELECT * FROM $table WHERE id ={$temp['pid']}";
            $query = $this->db->query($sql);
            $result = array($query->row_array(),$temp);
        }
        
        return $result;
    }
    //根据rid获取工作经历
    public function getExperienceInfo($AccountID,$table,$field){
        $this->db->where("AccountID",$AccountID);
        $this->db->select($field);
        if($table == "bp_resume_education"){
            $this->db->order_by("graduatetime","desc");
        }else{
            $this->db->order_by("endtime","desc");
        }

        $query = $this->db->get($table);
        return $query->result_array();

    }
    //根据id获取综合信息
    public function getGeneralInfoById($id,$table){
        $this->db->where("id",$id);
        $query = $this->db->get($table);
        return $query->row_array();
    }

    /*//根据账号id获取技能标签
    public function getSkillTagList($AccountID){
        $sql = "SELECT
                    rs.id,rs.tagname,count(rst.id) cnt
                FROM
                    bp_resume_skilltag AS rs
                LEFT JOIN
                    bp_resume_skilltag_thumbup AS rst
                ON
                    rs.id = rst.skilltagID
                AND rs.toAccountID = {$AccountID}
                AND rs.isDel <> 1
                AND rst.isDel <> 1
                GROUP BY rs.id
                ORDER BY rs.addTime DESC";
        $query = $this->db->query($sql);
        return $query->result_array();
    }*/

     //根据账号id获取技能标签
    public function getSkillTagList($AccountID){
        $sql = "SELECT id,tagname ,toAccountID FROM bp_resume_skilltag WHERE toAccountID = {$AccountID} AND isDel <> 1 ORDER BY addTime DESC";
        $query = $this->db->query($sql);
        $result =  $query->result_array();
        foreach ($result as $key => $value) {
           $thumbup_sql = "SELECT count(*) cnt from bp_resume_skilltag_thumbup where skilltagID={$value['id']} AND isDel <> 1";
            $thumbup_query = $this->db->query($thumbup_sql);
            $thumbup_res = $thumbup_query->row_array();
            $result[$key]['cnt'] = $thumbup_res['cnt'];
        }
        return $result;
    }

    //删除技能标签
    public function delSkillTag($id = 0){
        $this->db->where('id',$id);
        $res = $this->db->update('bp_resume_skilltag',array('isDel'=>1));
        return $res;
    }

/**
 * 修改用户停用启用状态
 * @param  [type] $table [description]
 * @param  array  $data  [description]
 * @return [type]        [description]
 */
   public function editStatus($table,$data = array()){
        $this->db->where("AccountID",$data['id']);
        $datas = array(
            'Status'=>$data['Status']
        );

        $res = $this->db->update($table,$datas);
        return $res;
    }


    public function getMyNewsEducation($AccountID){
        $sql = "SELECT * FROM bp_resume_education WHERE  graduatetime = (SELECT  MAX(graduatetime) FROM bp_resume_education WHERE AccountID = {$AccountID}) AND AccountID = {$AccountID} limit 1";
        $query = $this->db->query($sql);
        return $query->row_array();
    }


    /**
     *
     * 通过用户名获取用户信息
     */
    public function get_userinfo_by_username($username)
    {
        $sql = "SELECT  u.* ,b.name FROM bp_user u,bp_resume_baseinfo b WHERE u.AccountID = b.AccountID AND u.Username = {$username}";
        $query = $this->db->query($sql);
        if($query->num_rows()>0)
        {
            $res=$query->row_array();
            return $res;
        }
        return null;
    }

  

}


